<?php
namespace Modules\ShopApi\Models;
use Modules\AuthAdmin\Services\CommonService;
use Modules\AuthAdmin\Models\AuthImage as AuthImageModel;
use Modules\ShopApi\Models\ShopConsultLike as ShopConsultLikeModel;
use Modules\ShopApi\Services\ShopUserService;
use Illuminate\Support\Facades\DB;
class ShopConsult extends BaseApiModel
{	 
	/**
     * @name  商品列表
     */
	public function get_consult($data){
		$model = $this->query();
		$model = $model->where('status',1);
		if(isset($data['key']) && $data['key'] != ''){
			$model = $model->where('title','like','%' . $data['key'] . '%');
		}
		$list = $model->select('id','title','author','images','reading_quantity')
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->paginate(10)
				->toArray();		
		$list['data'] = $this->images_list($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);			
	}
	/**
     * @name  咨询详情
     */
	public function get_consult_detail($id){
		$this->where(['id'=>$id])->increment('reading_quantity',1);
		$CommonService = new CommonService();
		$user_info = (new ShopUserService())->my();
		$info = $this->select('id','title','author','reading_quantity','like_number','content','created_at')
				->where('id',$id)
				->first()
				->toArray();
		$info['created_at'] = $CommonService->format_time(strtotime($info['created_at']));
		$info['like_number_status'] = false;	
		$info['content'] = $CommonService->replace_pic_url($info['content']);	
		if($user_info){
			$info['like_number_status'] = ShopConsultLikeModel::where(['user_id'=>$user_info['id'],'consult_id'=>$id])->count()?true:false;
		}	
		return $this->api_success(['data'=>$info]);	
	}
	/**
     * @name  图片处理
     */
	 private function images_list($data){
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		foreach($data as $k=>$v){
			
			$data[$k]['images_arr'] = [];
			if($v['images']){
				$v['images'] = AuthImageModel::whereIn('id',explode('|',$v['images']))->select('id','url','open')->get();	
				if($v['images']){
					$v['images'] = $v['images']->toArray();
					foreach($v['images'] as $k1=>$v1){
						if($v1['open'] == 1){
							$data[$k]['images_arr'][] = $http . $v1['url'];
						}else{
							$data[$k]['images_arr'][] = $v1['url'];
						}
					}
				}
			}
		}
		return $data;
	 }
	/**
     * @name  咨询点赞
     */
	 public function set_consult_like($id){
		$user_info = (new ShopUserService())->my();
		if(ShopConsultLikeModel::where(['user_id'=>$user_info['id'],'consult_id'=>$id,])->count()){
			return $this->api_success();
		}
		$like_number = $this->where(['id'=>$id])->value('like_number');
		DB::beginTransaction();
		try{
			$this->where(['id'=>$id])->update([
				'like_number'=>$like_number+1
			]);
			ShopConsultLikeModel::insert([
				'user_id'=>$user_info['id'],
				'consult_id'=>$id,
				'created_at'=>date('Y-m-d H:i:s')
			]);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	 } 
}
